﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using shop.cart;

namespace comix
{
    public partial class home : System.Web.UI.Page
    {
        protected BooksCollection bc;


        protected override void  OnLoadComplete(EventArgs e)
{
 	 base.OnLoadComplete(e);

     LCustomer user = (LCustomer)Session["customer"];
            

            int val = -1;
            if (Request.QueryString["ser_id"] != null)
            {
                Session["seltype"] = 0;
                val = Convert.ToInt32(Request.QueryString["ser_id"]);
            }
            if (Request.QueryString["pub_id"] != null)
            {
                Session["seltype"] = 1;
                val = Convert.ToInt32(Request.QueryString["pub_id"]);
            }
            if (Request.QueryString["cat_id"] != null)
            {
                Session["seltype"] = 2;
                val = Convert.ToInt32(Request.QueryString["cat_id"]);
            }
            if (Request.QueryString["aut_id"] != null)
            {
                Session["seltype"] = 3;
                val = Convert.ToInt32(Request.QueryString["aut_id"]);
            }

            bc = new BooksCollection();

            string s = "";
            if(user.Id == -1)
                s = "SELECT [bok_id], [bok_title], [ptr_total], [bok_hasdemo], [bok_description],[bok_rating], 0 as bok_owned FROM [BOOKS] INNER JOIN TIERS on ptr_id = bok_ptr_id ";
            else
                s = "SELECT [bok_id], [bok_title], [ptr_total], [bok_hasdemo], [bok_description],[bok_rating], CASE (SELECT COUNT(*) FROM BOOKS2USERS WHERE u2b_bok_id = bok_id AND u2b_usr_id = "+user.Id.ToString()+" AND u2b_issample = 0) WHEN 0 THEN 0 ELSE 1 END as bok_owned FROM [BOOKS] INNER JOIN TIERS on ptr_id = bok_ptr_id ";
                 

            string f = "";
            string lblsql = "";
            if (val != -1)
            {
                switch ((int)Session["seltype"])
                {
                    case 0:
                        lbl_header.Text = "Σειρά: ";
                        f = "WHERE bok_ser_id = " + val.ToString() +" AND bok_deleted = 0";
                        lblsql = "SELECT ser_name FROM Series WHERE ser_id = " + val.ToString();
                        break;
                    case 1:
                        lbl_header.Text = "Εκδότης: ";
                        f = " INNER JOIN SERIES on bok_ser_id = ser_id WHERE ser_pub_id = " + val.ToString() + " AND bok_deleted = 0";
                        lblsql = "SELECT pub_name FROM Publishers WHERE pub_id = " + val.ToString();
                        break;
                    case 2:
                        lbl_header.Text = "Είδος: ";
                        f = " INNER JOIN SERIES on bok_ser_id = ser_id WHERE ser_cat_id = " + val.ToString() + " AND bok_deleted = 0";
                        lblsql = "SELECT cat_name FROM Categories WHERE cat_id = " + val.ToString();
                        break;
                    case 3:
                        lbl_header.Text = "Δημιουργός: ";
                        f = " INNER JOIN BOOKS2AUTHORS on a2b_bok_id = bok_id WHERE a2b_aut_id = " + val.ToString() + " AND bok_deleted = 0";
                        lblsql = "SELECT ISNULL(aut_firstname,'')+' '+aut_lastname FROM Authors WHERE aut_id = " + val.ToString();
                        break;
                }
            }

            s += f;

            string sorting = "";
            switch((int)Session["sortingType"])
            {
                case 1:
                    sorting = "bok_title";
                    break;
                case 2:
                    sorting = "bok_date desc";
                    break;
                case 3:
                    sorting = "bok_rating desc, bok_sales desc";
                    break;
            }
            
            bc.Command.CommandText = s + " ORDER BY " + sorting;
            bc.FillItem();
            vitrinacontroll1.ncolumns = 3;
            vitrinacontroll1.bc = bc;
            vitrinacontroll1.Update();

            if (val == -1)
                lbl_header.Text = "Όλα τα κόμιξ";
            else
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["mainconectionstring"].ConnectionString);
                SqlCommand com = new SqlCommand(lblsql, con);
                con.Open();
                string valstring = com.ExecuteScalar().ToString();
                con.Close();
                lbl_header.Text += valstring;
            }
        }

 
    }
}
